package com.myCommon.security.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.myCommon.security.entity.LoginUser;
import com.myCommon.security.entity.User;
import com.myCommon.security.mapper.UserMapper;
import com.myCommon.util.customerException.CustomerExceptionHandler;
import com.myCommon.util.jwt.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class UserDetailServiceImpl implements UserDetailsService {
      
      @Autowired
      private UserMapper userMapper;
      
      @Override
      public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userLambdaQueryWrapper.eq(User::getUserName, username);
            User user = userMapper.selectOne(userLambdaQueryWrapper);
            if (Objects.isNull(user)) {
                  throw new CustomerExceptionHandler("用户名:" + username + "不存在");
            }
      
            //todo 权限查询
            List<String> userAuthList = userMapper.getUserAuthByUserId(user.getId());
      
            return new LoginUser(user,userAuthList);
      }
}
